package com.kara.woodAgent.agent.model.impl;

import com.kara.woodAgent.agent.model.ModelProvider;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

/**
 * @author tzy
 * @version 1.0
 * @project wood_agent
 * @description
 * @date 2025/6/11 10:04:54
 */
@ConditionalOnProperty(prefix = "model.use",name = "enable",havingValue = "dashscope_open")
@Component
public class DashscopeOpenAiModelProvider implements ModelProvider {


	@Value("${model.dashscope_open.key}")
	private String apiKey;

	@Value("${model.dashscope_open.baseUrl}")
	private String baseUrl;

	@Value("${model.dashscope_open.name}")
	private String model;


	@Value("${model.dashscope_open.name.plan}")
	private String modelPlan;

	@Value("${model.dashscope_open.name.math}")
	private String modelMath;

	@Value("${model.dashscope_open.name.summary}")
	private String modelSummary;

	@Value("${model.dashscope_open.name.code}")
	private String modelCode;


	private OpenAiChatModel chatModel;

	private OpenAiChatModel chatModelPlan;


	private OpenAiChatModel chatModelSummary;


	private OpenAiChatModel chatModelCode;

	private OpenAiChatModel chatModelMath;

	@PostConstruct
	public void init() {
		chatModel = OpenAiChatModel.builder()
				.apiKey(apiKey)
				.baseUrl(baseUrl)
				.modelName(model)
				.build();

		chatModelPlan = OpenAiChatModel.builder()
				.apiKey(apiKey)
				.baseUrl(baseUrl)
				.modelName(modelPlan)
				.build();

		chatModelSummary = OpenAiChatModel.builder()
				.apiKey(apiKey)
				.baseUrl(baseUrl)
				.modelName(modelSummary)
				.build();


		chatModelCode = OpenAiChatModel.builder()
				.apiKey(apiKey)
				.baseUrl(baseUrl)
				.modelName(modelCode)
				.build();

		chatModelMath = OpenAiChatModel.builder()
				.apiKey(apiKey)
				.baseUrl(baseUrl)
				.modelName(modelMath)
				.build();

	}

	@Override
	public ChatLanguageModel getChatModel(String type) {

		if (type == null) {
			return chatModel;
		}

		return switch (type) {
			case "plan" -> chatModelPlan;
			case "summary" -> chatModelSummary;
			case "code" -> chatModelCode;
			case "math" -> chatModelMath;
			default -> chatModel;
		};
	}
}
